#!/usr/bin/env python
# -*- coding: utf-8 -*-
# valid coding options: iso-8859-1, utf-8
# ------------------------------------------------------------------------------
# <++name of the game++>.py
# ------------------------------------------------------------------------------
# Copyright (C) 2010 by Andreas Kuntz <akuntz@tm.uka.de>
# All rights reserved.
# 
"""\
<++description++>
"""

import sys, os

__author__      = "Andreas Kuntz <akuntz@tm.uka.de>"
__version__     = "0.0.1"
__date__        = "$Date$"
__svn_id__      = "$Id$"

input 		= sys.stdin
output 		= sys.stdout
verbose 	= False

## configuration
##	gst-launch-0.10 filesrc location=/path/to/file/beep.mp3 ! mad ! audioconvert ! audioresample ! alsasink
##	gst-launch-0.10 filesrc location= /usr/share/sounds/phone.wav ! wavparse ! audioconvert ! audioresample ! alsasink

player_cmd = "gst-launch-0.10 filesrc location=%s ! %s ! audioconvert ! audioresample ! alsasink"

codec = {
	"mp3":"mad",
	"wav":"wavparse"
	}

def usage():
    print """\
usage: %s [options] file.mp3 file.wav
    -v --verbose	be verbose
    -h --help		show this help
"""%os.path.basename(sys.argv[0])

def main():
    global input, output, verbose

    import getopt
    opts, args = getopt.getopt(sys.argv[1:], "vh", ["verbose", "help"])
    for o, a in opts:
	if o in "-v --verbose".split():
	    verbose = True
	elif o in "-h --help".split():
	    usage()
	    sys.exit()

    for fn in args:
	try:
	    base, ext = fn.rsplit(".", 1)
	    assert ext in codec.keys(), "unknown codec for extension " + repr(ext)
	    cmd = player_cmd%(fn, codec[ext])
	    print "executing:", repr(cmd)
	    os.system(cmd)
	except ValueError:
	    print "spipping file or dir", repr(fn)
	except AssertionError:
	    print "skipping file", repr(fn), "available codecs: ", codec.keys()

    if verbose: 
	print >> output, "input=%s"%input.name, "output=%s"%output.name
    


if __name__ == '__main__':
    main()
